﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Neotic.DataAccess.Sql
{
    public class CustomSqlColumn : ISelectColumn
    {
        protected CustomSqlColumn() { }

        protected string[] _formatter;
        protected Column[] _columns;
        public object Tag { get; set; }
        protected string _aliasName;
        
        /// <summary>
        /// Les éléments des tableaux formatter et columns sont utilisés par alternance en commençant par formatter
        /// formatter[0], columns[0], formatter[1], columns[1], ... 
        /// </summary>
        /// <param name="formatter"></param>
        /// <param name="columns"></param>
        public CustomSqlColumn(string[] formatter, Column[] columns)
        {
            _formatter = formatter;
            _columns = columns;
        }


        public virtual string FullName
        {
            get {
                string fullName = string.Empty;
                for (int i = 0; i < Math.Max(_formatter.Length, _columns.Length); i++)
                {
                    if ((i < _formatter.Length) && (!string.IsNullOrEmpty(_formatter[i])))
                        fullName = string.Concat(fullName, _formatter[i]);
                    if (i < _columns.Length)
                    {
                        Column col = null;
                        col = _columns[i];
                        if (!object.Equals(col, null))
                            fullName = string.Concat(fullName, col.FullName);
                    }
                }
                return fullName;
            }
        }


        public string AliasName
        {
            get { return string.IsNullOrWhiteSpace(_aliasName) ? this.FullName : _aliasName; }
        }

        public string FullAliasName
        {
            get
            {
                return (string.IsNullOrWhiteSpace(_aliasName) ? this.FullName : string.Concat(this.FullName, " as '", _aliasName, "'"));
            }
        }

        public ISelectColumn As(string aliasName)
        {
            _aliasName = aliasName;
            return this;
        }


        public OrderClauseColumn Ascending { get { return new OrderClauseColumn(this, true); } }
        public OrderClauseColumn Descending { get { return new OrderClauseColumn(this, false); } }


    }
}
